using System;
using System.Collections.Generic;
using System.Text;
using UserInterface.MML.Base;

namespace UserInterface.MML
{
    public static class Manager
    {
        public static void Initialize(string emoticonsFile)
        {
            List<string> allowedTagNames = new List<string>();
            allowedTagNames.Add("b");
            allowedTagNames.Add("i");
            allowedTagNames.Add("u");
            allowedTagNames.Add("s");
            allowedTagNames.Add("quote");
            allowedTagNames.Add("url");
            allowedTagNames.Add("img");
            allowedTagNames.Add("code");
            allowedTagNames.Add("size");
            allowedTagNames.Add("spoiler");
            allowedTagNames.Add("search");
            allowedTagNames.Add("google");
            allowedTagNames.Add("attachment");
            Scanner.AllowedTagNames = allowedTagNames;

            List<string> selfClosingTagNames = new List<string>();
            selfClosingTagNames.Add("img");
            selfClosingTagNames.Add("url");
            selfClosingTagNames.Add("search");
            selfClosingTagNames.Add("google");
            selfClosingTagNames.Add("attachment");
            Parser.SelfClosingTagNames = selfClosingTagNames;

            Compiler.RegisterTagHandler("b", new BasicHTMLTagHandler());
            Compiler.RegisterTagHandler("i", new BasicHTMLTagHandler());
            Compiler.RegisterTagHandler("s", new BasicHTMLTagHandler());
            Compiler.RegisterTagHandler("u", new BasicHTMLTagHandler());

            Compiler.RegisterTagHandler("quote", new QuoteTagHandler());
            Compiler.RegisterTagHandler("url", new URLTagHandler());
            Compiler.RegisterTagHandler("img", new ImageTagHandler());
            Compiler.RegisterTagHandler("code", new CodeTagHandler());
            Compiler.RegisterTagHandler("size", new SizeTagHandler());
            Compiler.RegisterTagHandler("spoiler", new SpoilerTagHandler());
            Compiler.RegisterTagHandler("search", new SearchTagHandler());
            Compiler.RegisterTagHandler("google", new GoogleTagHandler());
            Compiler.RegisterTagHandler("attachment", new AttachmentTagHandler());

            TextFilterManager.RegisterFilter(new HTMLEntitiesTextFilter());
            TextFilterManager.RegisterFilter(new NewLineToBreakTextFilter());
            TextFilterManager.RegisterFilter(new EmoticonTextFilter(emoticonsFile));
        }

        public static string Process(string input, object context)
        {
            Scanner scanner = new Scanner(input);
            Parser parser = new Parser(scanner.Tokens);
            return Compiler.Compile(parser.ParseTree, context);
        }
    }
}
